package com.lizhi.updata;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Set;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lizhi.model.FilmModel;
import com.lizhi.util.Util;

public class ChangeFilmTime {
	static final String originURL = "https://freebook.firebaseio.com/films.json";
	static final String url = "https://freebook.firebaseio.com/films.json";

	public static void main(String[] args) {
		try {
			CloseableHttpClient httpClient = HttpClients.createDefault();
			HttpGet httpPost = new HttpGet(originURL);
			CloseableHttpResponse response = httpClient.execute(httpPost);
			InputStream inputStream = response.getEntity().getContent();
			String str = Util.readStream(inputStream);
			JSONObject jsonObject = JSON.parseObject(str);
			List<com.lizhi.model.FilmModel> filmModels = new ArrayList<com.lizhi.model.FilmModel>();
			Set<String> keySet = jsonObject.keySet();
			for (String string : keySet) {
				FilmModel filmModel = JSON.toJavaObject(jsonObject.getJSONObject(string), FilmModel.class);
				filmModels.add(filmModel);
			}

			Util.clearData(originURL);
			Calendar calendar = Calendar.getInstance();
			calendar.set(2015, 3, 15, 10, 16);

			int i = 0;
			for (FilmModel filmModel : filmModels) {
				filmModel.time = String.valueOf(calendar.getTimeInMillis());
				calendar.add(Calendar.MILLISECOND, 1);
				if (i % 4 == 0) {
					calendar.add(Calendar.DAY_OF_MONTH, 1);
				}
				System.out.println(JSON.toJSONString(filmModel));
				new UploadDataTask(url, JSON.toJSONString(filmModel)).start();
				i++;
			}

		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
	}

}
